package Q5;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) {
        Scanner sc = null;
        Socket socket = null;
        ObjectOutputStream objectOutputStream = null;
        ClientRecieveThread clientRecievedThread = null;
        try {
            sc = new Scanner(System.in);
            socket = new Socket("127.0.0.1",8889);
            objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
            System.out.println("链接成功");
            System.out.println("输入用户名");
            String name = sc.next();
            System.out.println("聊天----1， 传文件----2");
            int choose = sc.nextInt();
            clientRecievedThread = new ClientRecieveThread(socket);
            clientRecievedThread.setDaemon(true);
            clientRecievedThread.start();
            if(1 == choose ) {
                while (true) {
                    Thread.sleep(100);
                    System.out.println("请输入要发送的内容");
                    String content = sc.next();
                    objectOutputStream.writeObject(new Message("chat", name, content));
                    System.out.println("消息发送成功");
                    if("bye".equals(content)) {
                        System.out.println("聊天结束");
                        break;
                    }
                }
            }
            if(2 == choose ) {
                while (true) {
                    Thread.sleep(100);
                    System.out.println("请输入要发送的文件");
                    File file = new File(sc.next());
                    if(file.exists()) {
                       String filename = file.getName();// content
                        FileInputStream fileInputStream = null;
                        try {
                            fileInputStream = new FileInputStream(file);
                            Util.filetoSocketMessageStream(objectOutputStream,fileInputStream,filename, name);
                        } catch (IOException e) {
                            throw e;
                        } finally {
                            assert fileInputStream != null;
                            fileInputStream.close();
                        }
                    }
                    else{
                        System.out.println("文件不存在");
                        break;
                    }
                }
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
            return;
        } finally {
            assert sc != null;
            assert objectOutputStream != null;
            sc.close();
            try {
                objectOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            clientRecievedThread.interrupt();
        }

    }
}
